package org.jeecg.modules.door.controller;

import com.alibaba.fastjson.JSON;
import org.jeecg.common.system.query.QueryGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.door.entity.LwgyJointDoor;
import org.jeecg.modules.door.entity.LwgyDoorGrouppassword;
import org.jeecg.modules.door.entity.LwgyDoorGroupcard;
import org.jeecg.modules.door.entity.LwgyDoorGroupright;
import org.jeecg.modules.door.service.ILwgyDoorGrouprightService;
import org.jeecg.modules.door.service.ILwgyJointDoorService;
import org.jeecg.modules.door.service.ILwgyDoorGrouppasswordService;
import org.jeecg.modules.door.service.ILwgyDoorGroupcardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

 /**
 * @Description: 分组管理
 * @Author: jeecg-boot
 * @Date:   2023-08-14
 * @Version: V1.0
 */
@Api(tags="分组管理")
@RestController
@RequestMapping("/door/lwgyDoorGroupright")
@Slf4j
public class LwgyDoorGrouprightController extends JeecgController<LwgyDoorGroupright, ILwgyDoorGrouprightService> {

	@Autowired
	private ILwgyDoorGrouprightService lwgyDoorGrouprightService;

	@Autowired
	private ILwgyJointDoorService lwgyJointDoorService;

	@Autowired
	private ILwgyDoorGrouppasswordService lwgyDoorGrouppasswordService;

	@Autowired
	private ILwgyDoorGroupcardService lwgyDoorGroupcardService;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询
	 * @param lwgyDoorGroupright
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "分组管理-分页列表查询")
	@ApiOperation(value="分组管理-分页列表查询", notes="分组管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyDoorGroupright lwgyDoorGroupright,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyDoorGroupright> queryWrapper = QueryGenerator.initQueryWrapper(lwgyDoorGroupright, req.getParameterMap());
		Page<LwgyDoorGroupright> page = new Page<LwgyDoorGroupright>(pageNo, pageSize);
		IPage<LwgyDoorGroupright> pageList = lwgyDoorGrouprightService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
     *   添加
     * @param lwgyDoorGroupright
     * @return
     */
    @AutoLog(value = "分组管理-添加")
    @ApiOperation(value="分组管理-添加", notes="分组管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LwgyDoorGroupright lwgyDoorGroupright) {
        lwgyDoorGrouprightService.save(lwgyDoorGroupright);
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     * @param lwgyDoorGroupright
     * @return
     */
    @AutoLog(value = "分组管理-编辑")
    @ApiOperation(value="分组管理-编辑", notes="分组管理-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LwgyDoorGroupright lwgyDoorGroupright) {
        lwgyDoorGrouprightService.updateById(lwgyDoorGroupright);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @AutoLog(value = "分组管理-通过id删除")
    @ApiOperation(value="分组管理-通过id删除", notes="分组管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        lwgyDoorGrouprightService.delMain(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @AutoLog(value = "分组管理-批量删除")
    @ApiOperation(value="分组管理-批量删除", notes="分组管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.lwgyDoorGrouprightService.delBatchMain(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyDoorGroupright lwgyDoorGroupright) {
        return super.exportXls(request, lwgyDoorGroupright, LwgyDoorGroupright.class, "分组管理");
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyDoorGroupright.class);
    }
	/*---------------------------------主表处理-end-------------------------------------*/
	

    /*--------------------------------子表处理-门锁管理-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	@AutoLog(value = "门锁管理-通过主表ID查询")
	@ApiOperation(value="门锁管理-通过主表ID查询", notes="门锁管理-通过主表ID查询")
	@GetMapping(value = "/listLwgyJointDoorByMainId")
    public Result<?> listLwgyJointDoorByMainId(LwgyJointDoor lwgyJointDoor,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LwgyJointDoor> queryWrapper = new QueryWrapper<LwgyJointDoor>();
		queryWrapper.eq("group_id",lwgyJointDoor.getGroupId());
        Page<LwgyJointDoor> page = new Page<LwgyJointDoor>(pageNo, pageSize);
        IPage<LwgyJointDoor> pageList = lwgyJointDoorService.queryPageListCustom(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param lwgyJointDoor
	 * @return
	 */
	@AutoLog(value = "门锁管理-添加")
	@ApiOperation(value="门锁管理-添加", notes="门锁管理-添加")
	@PostMapping(value = "/addLwgyJointDoor")
	public Result<?> addLwgyJointDoor(@RequestBody LwgyJointDoor lwgyJointDoor) {
		if(oConvertUtils.isEmpty(lwgyJointDoor.getRoomId()) && oConvertUtils.isEmpty(lwgyJointDoor.getDoorNo())){
			return Result.error("房间信息和门锁标识至少添加一个");
		}
		//通过房间信息或者标识信息获得门锁信息，得到门锁id ,然后把id
		QueryWrapper<LwgyJointDoor> queryWrapper = new QueryWrapper<LwgyJointDoor>();
		List<LwgyJointDoor> list = new ArrayList<LwgyJointDoor>();
		if(oConvertUtils.isNotEmpty(lwgyJointDoor.getRoomId())){
			queryWrapper.eq("room_id",lwgyJointDoor.getRoomId());
			list = lwgyJointDoorService.list(queryWrapper);
		}
		if(oConvertUtils.isNotEmpty(lwgyJointDoor.getDoorNo())){
			queryWrapper.eq("door_no",lwgyJointDoor.getDoorNo());
			list = lwgyJointDoorService.list(queryWrapper);
		}
		if(list.size()>0){
			for(LwgyJointDoor t:list){
				t.setGroupId(lwgyJointDoor.getGroupId());
				System.out.println("lwgyJointD00r:"+lwgyJointDoor);
				lwgyJointDoorService.updateById(t);

			}
			return Result.OK("添加成功！");
		}
		return Result.error("没找到相应的门锁。无法添加到组");

	}

    /**
	 * 编辑
	 * @param lwgyJointDoor
	 * @return
	 */
	@AutoLog(value = "门锁管理-编辑")
	@ApiOperation(value="门锁管理-编辑", notes="门锁管理-编辑")
	@PutMapping(value = "/editLwgyJointDoor")
	public Result<?> editLwgyJointDoor(@RequestBody LwgyJointDoor lwgyJointDoor) {
		lwgyJointDoorService.updateById(lwgyJointDoor);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "门锁管理-通过id删除")
	@ApiOperation(value="门锁管理-通过id删除", notes="门锁管理-通过id删除")
	@DeleteMapping(value = "/deleteLwgyJointDoor")
	public Result<?> deleteLwgyJointDoor(@RequestParam(name="id",required=true) String id) {
		lwgyJointDoorService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "门锁管理-批量删除")
	@ApiOperation(value="门锁管理-批量删除", notes="门锁管理-批量删除")
	@DeleteMapping(value = "/deleteBatchLwgyJointDoor")
	public Result<?> deleteBatchLwgyJointDoor(@RequestParam(name="ids",required=true) String ids) {
	    this.lwgyJointDoorService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportLwgyJointDoor")
    public ModelAndView exportLwgyJointDoor(HttpServletRequest request, LwgyJointDoor lwgyJointDoor) {
		 // Step.1 组装查询条件
		 QueryWrapper<LwgyJointDoor> queryWrapper = QueryGenerator.initQueryWrapper(lwgyJointDoor, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<LwgyJointDoor> pageList = lwgyJointDoorService.list(queryWrapper);
		 List<LwgyJointDoor> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "门锁管理"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, LwgyJointDoor.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("门锁管理报表", "导出人:" + sysUser.getRealname(), "门锁管理"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importLwgyJointDoor/{mainId}")
    public Result<?> importLwgyJointDoor(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<LwgyJointDoor> list = ExcelImportUtil.importExcel(file.getInputStream(), LwgyJointDoor.class, params);
				 for (LwgyJointDoor temp : list) {
                    temp.setGroupId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 lwgyJointDoorService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-门锁管理-end----------------------------------------------*/

    /*--------------------------------子表处理-分组密码管理-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	@AutoLog(value = "分组密码管理-通过主表ID查询")
	@ApiOperation(value="分组密码管理-通过主表ID查询", notes="分组密码管理-通过主表ID查询")
	@GetMapping(value = "/listLwgyDoorGrouppasswordByMainId")
    public Result<?> listLwgyDoorGrouppasswordByMainId(LwgyDoorGrouppassword lwgyDoorGrouppassword,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LwgyDoorGrouppassword> queryWrapper = QueryGenerator.initQueryWrapper(lwgyDoorGrouppassword, req.getParameterMap());
        Page<LwgyDoorGrouppassword> page = new Page<LwgyDoorGrouppassword>(pageNo, pageSize);
        IPage<LwgyDoorGrouppassword> pageList = lwgyDoorGrouppasswordService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param lwgyDoorGrouppassword
	 * @return
	 */
	@AutoLog(value = "分组密码管理-添加")
	@ApiOperation(value="分组密码管理-添加", notes="分组密码管理-添加")
	@PostMapping(value = "/addLwgyDoorGrouppassword")
	public Result<?> addLwgyDoorGrouppassword(@RequestBody LwgyDoorGrouppassword lwgyDoorGrouppassword) {
		lwgyDoorGrouppasswordService.save(lwgyDoorGrouppassword);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param lwgyDoorGrouppassword
	 * @return
	 */
	@AutoLog(value = "分组密码管理-编辑")
	@ApiOperation(value="分组密码管理-编辑", notes="分组密码管理-编辑")
	@PutMapping(value = "/editLwgyDoorGrouppassword")
	public Result<?> editLwgyDoorGrouppassword(@RequestBody LwgyDoorGrouppassword lwgyDoorGrouppassword) {
		lwgyDoorGrouppasswordService.updateById(lwgyDoorGrouppassword);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "分组密码管理-通过id删除")
	@ApiOperation(value="分组密码管理-通过id删除", notes="分组密码管理-通过id删除")
	@DeleteMapping(value = "/deleteLwgyDoorGrouppassword")
	public Result<?> deleteLwgyDoorGrouppassword(@RequestParam(name="id",required=true) String id) {
		lwgyDoorGrouppasswordService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "分组密码管理-批量删除")
	@ApiOperation(value="分组密码管理-批量删除", notes="分组密码管理-批量删除")
	@DeleteMapping(value = "/deleteBatchLwgyDoorGrouppassword")
	public Result<?> deleteBatchLwgyDoorGrouppassword(@RequestParam(name="ids",required=true) String ids) {
	    this.lwgyDoorGrouppasswordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportLwgyDoorGrouppassword")
    public ModelAndView exportLwgyDoorGrouppassword(HttpServletRequest request, LwgyDoorGrouppassword lwgyDoorGrouppassword) {
		 // Step.1 组装查询条件
		 QueryWrapper<LwgyDoorGrouppassword> queryWrapper = QueryGenerator.initQueryWrapper(lwgyDoorGrouppassword, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<LwgyDoorGrouppassword> pageList = lwgyDoorGrouppasswordService.list(queryWrapper);
		 List<LwgyDoorGrouppassword> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "分组密码管理"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, LwgyDoorGrouppassword.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("分组密码管理报表", "导出人:" + sysUser.getRealname(), "分组密码管理"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importLwgyDoorGrouppassword/{mainId}")
    public Result<?> importLwgyDoorGrouppassword(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<LwgyDoorGrouppassword> list = ExcelImportUtil.importExcel(file.getInputStream(), LwgyDoorGrouppassword.class, params);
				 for (LwgyDoorGrouppassword temp : list) {
                    temp.setGroupId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 lwgyDoorGrouppasswordService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-分组密码管理-end----------------------------------------------*/

    /*--------------------------------子表处理-分组卡管理-begin----------------------------------------------*/
	/**
	 * 通过主表ID查询
	 * @return
	 */
	@AutoLog(value = "分组卡管理-通过主表ID查询")
	@ApiOperation(value="分组卡管理-通过主表ID查询", notes="分组卡管理-通过主表ID查询")
	@GetMapping(value = "/listLwgyDoorGroupcardByMainId")
    public Result<?> listLwgyDoorGroupcardByMainId(LwgyDoorGroupcard lwgyDoorGroupcard,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<LwgyDoorGroupcard> queryWrapper = QueryGenerator.initQueryWrapper(lwgyDoorGroupcard, req.getParameterMap());
        Page<LwgyDoorGroupcard> page = new Page<LwgyDoorGroupcard>(pageNo, pageSize);
        IPage<LwgyDoorGroupcard> pageList = lwgyDoorGroupcardService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

	/**
	 * 添加
	 * @param lwgyDoorGroupcard
	 * @return
	 */
	@AutoLog(value = "分组卡管理-添加")
	@ApiOperation(value="分组卡管理-添加", notes="分组卡管理-添加")
	@PostMapping(value = "/addLwgyDoorGroupcard")
	public Result<?> addLwgyDoorGroupcard(@RequestBody LwgyDoorGroupcard lwgyDoorGroupcard) {
		lwgyDoorGroupcardService.save(lwgyDoorGroupcard);
		return Result.OK("添加成功！");
	}

    /**
	 * 编辑
	 * @param lwgyDoorGroupcard
	 * @return
	 */
	@AutoLog(value = "分组卡管理-编辑")
	@ApiOperation(value="分组卡管理-编辑", notes="分组卡管理-编辑")
	@PutMapping(value = "/editLwgyDoorGroupcard")
	public Result<?> editLwgyDoorGroupcard(@RequestBody LwgyDoorGroupcard lwgyDoorGroupcard) {
		lwgyDoorGroupcardService.updateById(lwgyDoorGroupcard);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "分组卡管理-通过id删除")
	@ApiOperation(value="分组卡管理-通过id删除", notes="分组卡管理-通过id删除")
	@DeleteMapping(value = "/deleteLwgyDoorGroupcard")
	public Result<?> deleteLwgyDoorGroupcard(@RequestParam(name="id",required=true) String id) {
		lwgyDoorGroupcardService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "分组卡管理-批量删除")
	@ApiOperation(value="分组卡管理-批量删除", notes="分组卡管理-批量删除")
	@DeleteMapping(value = "/deleteBatchLwgyDoorGroupcard")
	public Result<?> deleteBatchLwgyDoorGroupcard(@RequestParam(name="ids",required=true) String ids) {
	    this.lwgyDoorGroupcardService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

    /**
     * 导出
     * @return
     */
    @RequestMapping(value = "/exportLwgyDoorGroupcard")
    public ModelAndView exportLwgyDoorGroupcard(HttpServletRequest request, LwgyDoorGroupcard lwgyDoorGroupcard) {
		 // Step.1 组装查询条件
		 QueryWrapper<LwgyDoorGroupcard> queryWrapper = QueryGenerator.initQueryWrapper(lwgyDoorGroupcard, request.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		 // Step.2 获取导出数据
		 List<LwgyDoorGroupcard> pageList = lwgyDoorGroupcardService.list(queryWrapper);
		 List<LwgyDoorGroupcard> exportList = null;

		 // 过滤选中数据
		 String selections = request.getParameter("selections");
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
		 } else {
			 exportList = pageList;
		 }

		 // Step.3 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 mv.addObject(NormalExcelConstants.FILE_NAME, "分组卡管理"); //此处设置的filename无效 ,前端会重更新设置一下
		 mv.addObject(NormalExcelConstants.CLASS, LwgyDoorGroupcard.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("分组卡管理报表", "导出人:" + sysUser.getRealname(), "分组卡管理"));
		 mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		 return mv;
    }

    /**
     * 导入
     * @return
     */
    @RequestMapping(value = "/importLwgyDoorGroupcard/{mainId}")
    public Result<?> importLwgyDoorGroupcard(HttpServletRequest request, HttpServletResponse response, @PathVariable("mainId") String mainId) {
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		 for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			 MultipartFile file = entity.getValue();// 获取上传文件对象
			 ImportParams params = new ImportParams();
			 params.setTitleRows(2);
			 params.setHeadRows(1);
			 params.setNeedSave(true);
			 try {
				 List<LwgyDoorGroupcard> list = ExcelImportUtil.importExcel(file.getInputStream(), LwgyDoorGroupcard.class, params);
				 for (LwgyDoorGroupcard temp : list) {
                    temp.setGroupId(mainId);
				 }
				 long start = System.currentTimeMillis();
				 lwgyDoorGroupcardService.saveBatch(list);
				 log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
				 return Result.OK("文件导入成功！数据行数：" + list.size());
			 } catch (Exception e) {
				 log.error(e.getMessage(), e);
				 return Result.error("文件导入失败:" + e.getMessage());
			 } finally {
				 try {
					 file.getInputStream().close();
				 } catch (IOException e) {
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.error("文件导入失败！");
    }

    /*--------------------------------子表处理-分组卡管理-end----------------------------------------------*/




}
